package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.elinkway.infinitemovies.listener.OnReportListener;
import com.elinkway.infinitemovies.utils.BitmapUtils;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.le123.ysdq.R;

/**
 * Created by zhaopeipei on 2016/4/27.
 */
public class ReportPhotoAdapter extends BaseAdapter{

    private Context mContext;
    private LayoutInflater inflater;
    private OnReportListener mOnReportListener;
    private String mAid;
    public ReportPhotoAdapter(Context context, String aid) {
        super();
        mAid = aid;
        mContext = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (BitmapUtils.selectBitmap.size() == 9) {
            return 9;
        }
        return BitmapUtils.selectBitmap.size() + 1;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.grid_item_photo, parent, false);
            holder = new ViewHolder();
            holder.image = (ImageView) convertView.findViewById(R.id.grid_photo_image);
            holder.deleteImage= (ImageView) convertView.findViewById(R.id.iv_report_delete_image);
            holder.relativeLayout = (RelativeLayout) convertView.findViewById(R.id.rl_report_delete_image);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position < BitmapUtils.selectBitmap.size()) {
                    BitmapUtils.selectBitmap.remove(position);
                }
                notifyDataSetChanged();
                if (mOnReportListener != null && BitmapUtils.selectBitmap.size() == 0) {
                    mOnReportListener.onReportListener(true);
                }
            }
        });
        if (position == BitmapUtils.selectBitmap.size()) {
            holder.image.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(),
                    R.drawable.icon_addpic_focused));
            if (position == 9) {
                holder.image.setVisibility(View.GONE);
            }
            holder.deleteImage.setVisibility(View.GONE);
        } else {
            if (position < BitmapUtils.selectBitmap.size()) {
                holder.image.setImageBitmap(BitmapUtils.selectBitmap.get(position).getBitmap());
            }
            holder.deleteImage.setVisibility(View.VISIBLE);
        }
        return convertView;
    }
    @Override
    public long getItemId(int arg0) {
        return 0;
    }
    @Override
    public Object getItem(int position) {
        return null;
    }

    public class ViewHolder {
        public ImageView image;
        public ImageView deleteImage;
        public RelativeLayout relativeLayout;
    }

    Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    notifyDataSetChanged();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    public void update() {
        LogUtils.e("", "report update");
        new Thread(new Runnable() {
            @Override
            public void run() {
                LogUtils.e("", "report update");
                if (BitmapUtils.max == BitmapUtils.selectBitmap.size()) {
                    Message message = new Message();
                    message.what = 1;
                    mHandler.sendMessage(message);
                    LogUtils.e("lengye", "report update " + BitmapUtils.selectBitmap.size());
                } else {
                    BitmapUtils.max += 1;
                    Message message = new Message();
                    message.what = 1;
                    mHandler.sendMessage(message);
                    LogUtils.e("lengye", "report update1 " + BitmapUtils.selectBitmap.size());
                }
            }
        }).start();

    }

    public void setReportListener(OnReportListener onReportListener) {
        mOnReportListener = onReportListener;
    }

}
